package com.appstore.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.appstore.common.utils.SecurityUtils;
import com.appstore.domain.entity.App;
import com.appstore.service.IDevAppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.appstore.common.annotation.Log;
import com.appstore.common.core.controller.BaseController;
import com.appstore.common.core.domain.AjaxResult;
import com.appstore.common.enums.BusinessType;
import com.appstore.common.utils.poi.ExcelUtil;
import com.appstore.common.core.page.TableDataInfo;

@Slf4j
@RestController
@RequestMapping("/appstoredev/app")
public class DevAppController extends BaseController
{
    @Autowired
    private IDevAppService devAppService;

    /**
     * 查询应用详情列表
     */
    @PreAuthorize("@ss.hasPermi('appstoredev:app:list')")
    @GetMapping("/list")
    public TableDataInfo list(App app)
    {
        startPage();
        app.setDeveloperId(SecurityUtils.getUserId());
        log.info("app is " + app);
//        List<App> list = devAppService.selectAppList(app);
        List<App> list = devAppService.selectAppListByCondition(app);
        return getDataTable(list);
    }

    /**
     * 导出应用详情列表
     */
    @PreAuthorize("@ss.hasPermi('appstoredev:app:export')")
    @Log(title = "应用详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, App app)
    {
        List<App> list = devAppService.selectAppList(app);
        ExcelUtil<App> util = new ExcelUtil<App>(App.class);
        util.exportExcel(response, list, "应用详情数据");
    }

    /**
     * 获取应用详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('appstoredev:app:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(devAppService.selectAppById(id));
    }

    /**
     * 新增应用详情
     */
    @PreAuthorize("@ss.hasPermi('appstoredev:app:add')")
    @Log(title = "应用详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody App app)
    {
        return toAjax(devAppService.insertApp(app));
    }

    /**
     * 修改应用详情
     */
    @PreAuthorize("@ss.hasPermi('appstoredev:app:edit')")
    @Log(title = "应用详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody App app)
    {
        return toAjax(devAppService.updateApp(app));
    }

    /**
     * 删除应用详情
     */
    @PreAuthorize("@ss.hasPermi('appstoredev:app:remove')")
    @Log(title = "应用详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(devAppService.deleteAppByIds(ids));
    }
}

